
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class fileSplit {



        public static void main(String[] args) throws FileNotFoundException, IOException {
            File sourceFile = new File("E:\\log\\application.log");
//        System.out.println(sourceFile.exists());

            System.out.println("进入");
            long fileLength = sourceFile.length() / 50;        //分一下每一个小文件的大小
            byte[] b = new byte[1024*1024];        //这个不解释 如果看不懂 就去看IO流去吧
            RandomAccessFile raf1 = new RandomAccessFile(sourceFile, "r");
            int len = -1;
            for(int i = 0; i < 50; i++) {
                System.out.println("第"+i+"个文件");
                String name = "E:\\log\\"+sourceFile.getName() + "." + (i+1);
                System.out.println("文件名"+name);
                File file = new File(name);
                file.createNewFile();
                RandomAccessFile raf2 = new RandomAccessFile(file, "rw");
                while((len = raf1.read(b)) != -1) {
                    raf2.write(b, 0, len);        //我觉的这样写比raf2.write(b);高明一些
                    if(raf2.length() > fileLength)        //如果太大了就不在这个子文件写了 换下一个
                        break;
                }
                raf2.close();
            }
            raf1.close();
        }
}
